package org.alex.nettychat.netty.custom.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.alex.nettychat.netty.custom.utils.SessionUtil;

/**

 * @author : alex
 */
public class AuthHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        if (SessionUtil.hasLogin(ctx.channel())) {
            // 校验通过后 就不需要此逻辑
            ctx.pipeline().remove(this);
            super.channelRead(ctx, msg);
        } else {
            ctx.channel().close();
        }
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        if (SessionUtil.hasLogin(ctx.channel())) {
            System.out.println("当前连接登录验证完毕, 无须再次验证, AuthHandler 被移除");
        } else {
            System.out.println("无登录验证，强制关闭连接");
        }
    }
}
